<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/

// No direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
 
class JVframeworkViewDefault extends JView {

    function __construct($config = array()) {
        parent::__construct($config);
    }

    function display($tpl = null) {
        $this->checkPlg();
        $mainframe     = JFactory::getApplication();        
        $option        = $mainframe->get('scope'); 
        $document =& JFactory::getDocument();         
        $defaultTmpl=& $this->get('DefaultTemplate');
        $themeList  =& $this->get( 'ThemeList');        
        $pKey       =& $this->get( 'Key' );            
        $pagination =& $this->get( 'Pagination' );
        $state      =& $this->get( 'state' );
        $filter     =& $this->get( 'Filterorder' );
        
        if($defaultTmpl != 'jv-framework'){
            $document->addScriptDeclaration("
            window.addEvent('domready', function() {
            	var answer = confirm('JV Framework is not currently set as your default template. Would you like to make it your default template??');
        		if(answer){
        			submitbutton('setdefaulttmpl');
        		}
            });");
            
        }
        
        /* Get the values from the state object that were inserted in the model's construct function */
        $lists['order_Dir'] = $state->get( $filter['order_Dir'] );
        $lists['order']     = $state->get( $filter['order'] );  
       
        $var = array(
            'THEME_NAME'        => JText::_('COM_JVFRAMEWORK_MANAGER_THEMES_TITLE'),
            'DEFAULT'           => JText::_('COM_JVFRAMEWORK_THEMES_HOME'),
            'ASSIGNED'          => JText::_('COM_JVFRAMEWORK_THEMES_ASSIGNED'),
            'VERSION'           => JText::_('COM_JVFRAMEWORK_THEMES_VERSION'),
            'CREATION_DATE'     => JText::_('COM_JVFRAMEWORK_THEMES_CREATION'),
            'AUTHOR'            => JText::_('COM_JVFRAMEWORK_THEMES_AUTHOR'),
            'STATUS'            => JText::_('COM_JVFRAMEWORK_THEMES_STATUS'),            
            'NUM'               => JText::_('#'),            
            'TOKEN'             => JHTML::_('form.token'),
            'ORDER'             => JHTML::_('input.textbox','',$filter['order'],$lists['order'],'',array('hidden'=>true)),
            'ORDER_DIR'         => JHTML::_('input.textbox','',$filter['order_Dir'],$lists['order_Dir'],'',array('hidden'=>true)),
            'FORM_LINK'         => 'index.php?option='.$option,
            'OPTION'            => $option,
            'PKEY'              => $pKey,
        );
        
        $row = array();
        $i = 0;
        
        if(is_array($themeList))
        foreach($themeList as $theme){
            $row[$i]=$theme;
            if(!isset($theme['home']) || $theme['home'] == 2){
                $row[$i]['status']=0; 
            } else {
                $row[$i]['status']=1; 
            } 
            $i++;          
        }
        
        JSubMenuHelper::addEntry(JText::_('COM_JVFRAMEWORK_MANAGER_THEMES'), 'index.php?option=com_jvframework');
        JSubMenuHelper::addEntry(JText::_('COM_JVFRAMEWORK_MANAGER_UPDATE'), 'index.php?option=com_jvframework&c=update');      
        JToolBarHelper::title(JText::_('COM_JVFRAMEWORK_MANAGER_THEMES'), 'thememanager.png');         
        JToolBarHelper::custom('active','active.png','active.png',JText::_('COM_JVFRAMEWORK_THEMES_ACTIVE'));
        JToolBarHelper::custom('deactive','deactive.png','deactive.png',JText::_('COM_JVFRAMEWORK_THEMES_DEACTIVE'));    
        JToolBarHelper::custom('default','default.png','default.png',JText::_('COM_JVFRAMEWORK_THEMES_DEFAULT'));
        JToolBarHelper::custom('duplicate','duplicate.png','duplicate.png',JText::_('COM_JVFRAMEWORK_THEMES_DUPLICATE')); 
        //JToolBarHelper::deleteListX();  
        $bar = & JToolBar::getInstance('toolbar');
		$bar->appendButton( 'Popup', 'upload', 'COM_JVFRAMEWORK_THEMES_UPTHEME', "index.php?option=com_jvframework&task=upload&tmpl=component", 440,320 );
        
        JHTML::_('behavior.tooltip'); 
        $document->addStyleSheet(JURI::base().'components/com_jvframework/assets/css/style.css'); 
        $document->addScript(JURI::base()."components/com_jvframework/assets/js/framework.js");        
        
        $this->assign($var); 
        $this->assignRef( 'lists', $lists );
        $this->assignRef('data',$row);     
        $this->assignRef('pagination',$pagination);

        parent::display($tpl);
    }
    
    function edit($tpl = null) {
        $mainframe     = JFactory::getApplication();        
        $option        = $mainframe->get('scope');
        $datas         = $this->get('Data');
        $cids = JRequest::getVar ( 'cid', array (0 ), 'method', 'array');        
        $data =& $this->get('Table');
        $data->load((string)$cids[0]);      
        $pKey       =& $this->get( 'Key' ); 
        $menu       =& $this->get('Menu');
        $paramlist  =& $this->get('ParamList');
        $themeFiles  = $this->get('ThemeFile');        
        $options = array();
        
        $options[] = JHTML::_('select.option','-1','Select file to edit');
        
        if(count($themeFiles)){
            foreach($themeFiles as $key => $files){
                $options[] = JHTML::_('select.option',$key,$key,'value', 'text', true );
                
                if(count($files) && is_array($files)){
                    foreach($files as $k => $file){
                        if(!in_array($file,array('index.html','thumbnail.jpg')))
                        if(is_array($file)){
                            foreach($file as $f){
                                $options[] = JHTML::_('select.option', $f.'|'.$key.DS.$k,'-- '.$k.' >> '.$f,'value', 'text');    
                            }
                            
                        }else{
                            $options[] = JHTML::_('select.option',$file.'|'.$key,'-- '.$file,'value', 'text');
                        }
                    }
                }               
            }
        }
        
        $themes = array();
        if(is_array($datas)){
            foreach($datas as $item){
                $themes[current(explode('|', $item->theme))] = current(explode('|', $item->theme));    
            } 
        }
        
        $op = array();
        foreach($themes as $theme){
            $op[] = JHTML::_('select.option',$theme,$theme,'value', 'text' );
        }
        
        $var = array(
            'GLOBAL'        =>$paramlist['global'],
            'LAYOUT'        =>$paramlist['layoutsetting'],
            'MENU'          =>$paramlist['menusystem'],
            'GFONT'         =>$paramlist['gfont'],
            'MOBILE'        =>$paramlist['mobile'],
            'THEMEFILE'     => JHTML::_('select.genericlist',$options,'fileList','onchange="jvAdmin.editLayoutFile(); return false;"' ),
            'THEMELIST'     => JHTML::_('select.genericlist',$op,'theme', '' ),
            'TOKEN'         => JHTML::_('form.token'),
            'FORM_LINK'     => 'index.php?option='.$option,
            'OPTION'        => $option,
            'PKEY'          => $pKey
                
        );
        
        JToolBarHelper::title(JText::_('COM_JVFRAMEWORK_MANAGER_THEME_EDIT').' ['.end(explode('|', $data->theme)).']', 'config.png');
        JToolBarHelper::save();
        JToolBarHelper::apply();
        JToolBarHelper::cancel();   
        JToolBarHelper::divider();
        JToolBarHelper::custom('reset', 'reset', 'reset', 'Reset to default', 0);
              
        JHTML::_('behavior.tooltip');
        JHTML::_('behavior.modal');
        JHTML::_('behavior.calendar');
        JHTML::_('input.formValidation()','com_jvframework');
        JHTML::_('behavior.formvalidation()'); 
               
        $document =& JFactory::getDocument(); 
        $document->addStyleSheet(JURI::base().'components/com_jvframework/assets/css/style.css'); 
        $document->addScript(JURI::base()."components/com_jvframework/assets/js/framework.js"); 
        $document->addScript(JURI::base()."components/com_jvframework/assets/js/codemirror/codemirror.js");         
        $document->addScriptDeclaration("
        var editor;
        window.addEvent('domready', function() {
            editor = CodeMirror.fromTextArea('codeEditor', {
                height: '600px',
                parserfile:[
                    \"parsexml.js\", \"parsecss.js\", \"tokenizejavascript.js\", \"parsejavascript.js\", \"parsehtmlmixed.js\"
                ],
                stylesheet: [
                    '".JURI::base().'components/'.$option."/assets/css/codemirror/xmlcolors.css', 
                    '".JURI::base().'components/'.$option."/assets/css/codemirror/jscolors.css',
                    '".JURI::base().'components/'.$option."/assets/css/codemirror/csscolors.css',
                    '".JURI::base().'components/'.$option."/assets/css/codemirror/phpcolors.css',
                ],
                path: '".JURI::base().'components/'.$option."/assets/js/codemirror/',
                continuousScanning: 500,
                lineNumbers: true,
                content: document.getElementById('codeEditor').value
            });

        });");
      
        $this->assign($var); 
        $this->assignRef('data',$data);  

        parent::display($tpl);
    }
    
    function upload($tpl = null) {
        $mainframe     = JFactory::getApplication();        
        $option        = $mainframe->get('scope'); 
        
        $var = array(
            'TOKEN'         => JHTML::_('form.token'),
            'FORM_LINK'     => 'index.php?option='.$option,
            'OPTION'        => $option
        );
            
        JToolBarHelper::title(JText::_('UPLOAD THEME'), 'thememanager.png');
        JToolBarHelper::back();
        
        JHTML::_('behavior.tooltip');        
        $document =& JFactory::getDocument();   
        $document->addStyleSheet(JURI::base().'components/com_jvframework/assets/css/style.css'); 
        $document->addScript(JURI::base()."components/com_jvframework/assets/js/framework.js");        
        
        $this->assign($var); 
        $this->assignRef('data',$data);  
        parent::display($tpl);
    }
    
    function checkPlg(){
        $db = JFactory::getDBO();
        $db->setQuery('SELECT enabled FROM #__extensions WHERE element=\'jvframework\'');
        $result = $db->loadResult(); 
        
        if(!isset($result)){
             JError::raiseWarning('','Please install plugin System JV Framework !');
        }elseif($result == 0){
             JError::raiseWarning('','Please enable plugin System JV Framework !');
        }
    }
    
}
